﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Imgalib.Utility
{
    public class FunctionUtility
    {
        public string GetUSStateNameFromStateCode(string statecode)
        {
            string stateName = string.Empty;
            Dictionary<string, string> attrs = new Dictionary<string, string>();
            attrs.Add("Alabama", "AL");
            attrs.Add("Alaska", "AK");
            attrs.Add("Alberta", "AB");
            attrs.Add("Arizona", "AZ");
            attrs.Add("Arkansas", "AR");
            attrs.Add("British Columbia", "BC");
            attrs.Add("California", "CA");
            attrs.Add("Colorado", "CO");
            attrs.Add("Connecticut", "CT");
            attrs.Add("Delaware", "DE");
            attrs.Add("District of Columbia", "DC");
            attrs.Add("Florida", "FL");
            attrs.Add("Georgia", "GA");
            attrs.Add("Hawaii", "HI");
            attrs.Add("Idaho", "ID");
            attrs.Add("Illinois", "IL");
            attrs.Add("Indiana", "IN");
            attrs.Add("Iowa", "IA");
            attrs.Add("Kansas", "KS");
            attrs.Add("Kentucky", "KY");
            attrs.Add("Louisiana", "LA");
            attrs.Add("Maine", "ME");
            attrs.Add("Manitoba", "MB");
            attrs.Add("Maryland", "MD");
            attrs.Add("Massachusetts", "MA");
            attrs.Add("Michigan", "MI");
            attrs.Add("Minnesota", "MN");
            attrs.Add("Mississippi", "MS");
            attrs.Add("Missouri", "MO");
            attrs.Add("Montana", "MT");
            attrs.Add("Nebraska", "NE");
            attrs.Add("Nevada", "NV");
            attrs.Add("New Brunswick", "NB");
            attrs.Add("New Hampshire", "NH");
            attrs.Add("New Jersey", "NJ");
            attrs.Add("New Mexico", "NM");
            attrs.Add("New York", "NY");
            attrs.Add("Newfoundland and Labrador", "NF");
            attrs.Add("North Carolina", "NC");
            attrs.Add("North Dakota", "ND");
            attrs.Add("Northwest Territories", "NT");
            attrs.Add("Nova Scotia", "NS");
            attrs.Add("Ohio", "OH");
            attrs.Add("Oklahoma", "OK");
            attrs.Add("Ontario", "ON");
            attrs.Add("Oregon", "OR");
            attrs.Add("Pennsylvania", "PA");
            attrs.Add("Prince Edward Island", "PE");
            attrs.Add("Puerto Rico", "PR");
            attrs.Add("Quebec", "QC");
            attrs.Add("Rhode Island", "RI");
            attrs.Add("Saskatchewan", "SK");
            attrs.Add("South Carolina", "SC");
            attrs.Add("South Dakota", "SD");
            attrs.Add("Tennessee", "TN");
            attrs.Add("Texas", "TX");
            attrs.Add("Utah", "UT");
            attrs.Add("Vermont", "VT");
            attrs.Add("Virginia", "VA");
            attrs.Add("Washington", "WA");
            attrs.Add("West Virginia", "WV");
            attrs.Add("Wisconsin", "WI");
            attrs.Add("Wyoming", "WY");
            attrs.Add("Yukon", "YT");

            foreach (KeyValuePair<string, string> item in attrs)
            {
                if (item.Value == statecode.ToUpper())
                {
                    stateName = item.Key;
                    break;
                }
            }

            return stateName;
        }

        public string GetUSStateCodeFromStateName(string statename)
        {
            string stateCode = string.Empty;
            Dictionary<string, string> attrs = new Dictionary<string, string>();
            attrs.Add("Alabama", "AL");
            attrs.Add("Alaska", "AK");
            attrs.Add("Alberta", "AB");
            attrs.Add("Arizona", "AZ");
            attrs.Add("Arkansas", "AR");
            attrs.Add("British Columbia", "BC");
            attrs.Add("California", "CA");
            attrs.Add("Colorado", "CO");
            attrs.Add("Connecticut", "CT");
            attrs.Add("Delaware", "DE");
            attrs.Add("District of Columbia", "DC");
            attrs.Add("Florida", "FL");
            attrs.Add("Georgia", "GA");
            attrs.Add("Hawaii", "HI");
            attrs.Add("Idaho", "ID");
            attrs.Add("Illinois", "IL");
            attrs.Add("Indiana", "IN");
            attrs.Add("Iowa", "IA");
            attrs.Add("Kansas", "KS");
            attrs.Add("Kentucky", "KY");
            attrs.Add("Louisiana", "LA");
            attrs.Add("Maine", "ME");
            attrs.Add("Manitoba", "MB");
            attrs.Add("Maryland", "MD");
            attrs.Add("Massachusetts", "MA");
            attrs.Add("Michigan", "MI");
            attrs.Add("Minnesota", "MN");
            attrs.Add("Mississippi", "MS");
            attrs.Add("Missouri", "MO");
            attrs.Add("Montana", "MT");
            attrs.Add("Nebraska", "NE");
            attrs.Add("Nevada", "NV");
            attrs.Add("New Brunswick", "NB");
            attrs.Add("New Hampshire", "NH");
            attrs.Add("New Jersey", "NJ");
            attrs.Add("New Mexico", "NM");
            attrs.Add("New York", "NY");
            attrs.Add("Newfoundland and Labrador", "NF");
            attrs.Add("North Carolina", "NC");
            attrs.Add("North Dakota", "ND");
            attrs.Add("Northwest Territories", "NT");
            attrs.Add("Nova Scotia", "NS");
            attrs.Add("Ohio", "OH");
            attrs.Add("Oklahoma", "OK");
            attrs.Add("Ontario", "ON");
            attrs.Add("Oregon", "OR");
            attrs.Add("Pennsylvania", "PA");
            attrs.Add("Prince Edward Island", "PE");
            attrs.Add("Puerto Rico", "PR");
            attrs.Add("Quebec", "QC");
            attrs.Add("Rhode Island", "RI");
            attrs.Add("Saskatchewan", "SK");
            attrs.Add("South Carolina", "SC");
            attrs.Add("South Dakota", "SD");
            attrs.Add("Tennessee", "TN");
            attrs.Add("Texas", "TX");
            attrs.Add("Utah", "UT");
            attrs.Add("Vermont", "VT");
            attrs.Add("Virginia", "VA");
            attrs.Add("Washington", "WA");
            attrs.Add("West Virginia", "WV");
            attrs.Add("Wisconsin", "WI");
            attrs.Add("Wyoming", "WY");
            attrs.Add("Yukon", "YT");

            foreach (KeyValuePair<string, string> item in attrs)
            {
                if (item.Key.ToLower() == statename.ToLower())
                {
                    stateCode = item.Value;
                    break;
                }
            }

            return stateCode;
        }

        /// <summary>
        /// Calculates the number of days to add to the given day of
        /// the week in order to return the next occurrence of the
        /// desired day of the week.
        /// </summary>
        /// <param name="current">The starting day of the week.</param>
        /// <param name="desired">The desired day of the week.</param>
        /// <returns>
        /// The number of days to add to <var>current</var> day of week
        /// in order to achieve the next <var>desired</var> day of week.
        /// </returns>
        public int DaysToAdd(DayOfWeek current, DayOfWeek desired)
        {

            int c = (int)current;
            int d = (int)desired;
            int n = (7 - c + d);
            return (n > 7) ? n % 7 : n;
        }

        /// <summary>
        /// Finds the next date whose day of the week equals the specified day of the week.
        /// </summary>
        /// <param name="startDate">The date to begin the search.</param>
        /// <param name="desiredDay"> The desired day of the week whose date will be returneed.</param>
        /// <returns>
        ///		The returned date occurs on the given date's week.
        ///		If the given day occurs before given date, the date for the
        ///		following week's desired day is returned.
        /// </returns>
        public static DateTime GetNextDateForDay(DateTime startDate, DayOfWeek desiredDay)
        {
            // Given a date and day of week,
            // find the next date whose day of the week equals the specified day of the week.
            return startDate.AddDays(new FunctionUtility().DaysToAdd(startDate.DayOfWeek, desiredDay));
        }
        
    }
}
